Physics 212E Classical and Modern Physics Spring 2016 1. Introduction VPython Class 2: Functions, Fields, and the dreaded ˆr In our study of electric fields, we start with a single point charge source q source located at some position r source, and we want to know the field created by this source at some observation point r obs. The relationship between the vectors is illustrated in Fig. 1. Note that this figure uses the conventions for axis labeling that is consistent with that of the default view in VPython; in many textbooks, the default view has z going up the page, y to the right, and x coming out of the page. The vector displacement between the source and the observation point is given by Coulomb s law then gives the field at r obs due to the source at r source : r r obs r source. (1) F E = kq ˆr, (2) r2 where ˆr is the unit vector in the direction of ˆr. Your first task will be to write a Python script that will display a point charge at some position r source along with an arrow representing the electric field at some other point r obs. As a specific simple, consider a point charge q source = 3 C on the x-axis with coordinates (3,0,0), and suppose I ask for the field at position with coordinates (3,2,0). Your Python script should display something like Fig. 2, with an an arrow with its tail at the observation point, pointing away from the source, and a length proportional to kq source /r 2 = k3/2 2 = 3k/4 (except your output will be much prettier, and you will be able change your perspective, and zoom in and out). In this exercise we will set k = 1. y ˆr observation point r obs r = r obs r source q source r source x z Figure 1: Vectors associated with the positions of a point charge source and the observation point for an electric field. 1
y 3 2 1 E = 3k 4 ĵ observation point 1 2 3 x z q source Figure 2: Example output. A source charge is located on the x-axis with coordinates (2,0,0), and we show the field vector E with its tail at the observation point with coordinates (2,3,0). 2. Getting Started Reminder: All Programs Python 3.2 IDLE (Python GUI) From the Python shell, select File New Window Now you ve got the IDLE window in which we ll write our scripts. The first line of your program will always (unless otherwise stated) be: from visual import * 3. Python Jargon: Objects and Attributes Before starting on the ultimate task of displaying field vectors, lets review some commands we used last time and learn how to extend them, while introducing some of the language of Python. The command particle = sphere(pos=vector(0,0,0),radius=0.5,color=color.red) creates a sphere object (or equivalently, an object in the sphere class) whose name is particle. We could give it any name we want (e.g., Fred or Ginger). The name is used as a way to identify the object that we just created, and we can create other objects in the same class class with other names. In creating the sphere above, we ve given it certain attributes, namely a position, a radius, and a color. The visual module automatically knows what to do with these attributes in this case, draw the sphere centered at the given position, with the given radius and color. We can also add other attributes to the object by hand, and call them whatever we want. For example: particle.vel = vector(0, 1, -2) 2
adds a vector property of velocity (vel) to the sphere object named particle. This is merely a convenient way for us to keep track of the velocity of this object. The computer doesn t automatically ascribe any special meaning to this attribute; (for example, it doesn t actually affect the speed of the particle in an animation); we have to do that by hand. We could also add this property to the object when we first define it, i.e., particle = sphere(pos=vector(0,0,0),vel=vector(0,1,-2),radius=0.5, color=color.red) Now try the following (one line at a time): print(particle.vel) print(particle.vel.x) print(particle.vel.y) print(particle.vel.z) print(particle.vel+7) print(particle.vel.x+7) If any statements give you errors, try to explain what s wrong with them before deleting them. Since we are eventually going to study the motion of charged particles in electric and magnetic fields, we will need include mass and charge as attributes of some of our objects. Again, we can add them after particle has been defined, using particle.mass = 1 particle.charge = 2 or we can add these attributes to the list when particle is defined. 4. The ˆr vector Now let s get down to the business of calculating and displaying field vectors. We ll start in this section by displaying ˆr vectors. From IDLE, open the file background.py that you can find in the PHYS 211E folder in our departmental public netspace. Save this with a new name in your space. Run this script, and you should see transparent x-y, x-z, and y-z planes that intersect at the origin, along with axis labels. The displayed plane sections are all 10 10 in VPython units. There is no fancy programming here I m just trying to save you some typing. You do not have to use this file as your starting point, but something like this helps me situate the objects we will be displaying in a Cartesian coordinate space. In the rest of this section I m not going to give you explicit commands to type. Rather, I would like you to try to use resources at your disposal, including the handout for VPython Class #1, any scripts you wrote last week, the Help in IDLE, or the online information at http://vpython.org/ contents/docs/primitives.html. None of this should take much time. If you get bogged down, just ask, and I ll give you much more explicit instructions. Create a sphere object at the origin representing a source charge with q = +4. Let s all agree to give this sphere object the name source. 3
The source object has several attributes. Which of these attributes corresponds to the vector r source? Put a temporary print statement into your script to check and make sure that you know how to get the vector r source in terms of your Python variables. (Hint: source is not itself equal to r source.) Define a observation point at (2,0,0) as a Python vector with the name obs. This observation point serves as r obs. (There is no real need to define this as a sphere object; it s just a point, with no radius, color, or charge attributes. But if you want to display it with a colored sphere, that s ok.) Determine a Python expression for the vector r in terms of your source and obs; I suggest calling this r. Determine a Python expression for the vector ˆr in terms of your source and obs; I suggest calling this rhat. Have VPython draw an arrow object at the position obs that represents ˆr. (I use the attribute shaftwidth=0.1). Do you get the arrow you expect? Change your observation point. Does the arrow representing ˆr change appropriately? Change the position of your source charge. Does the arrow representing ˆr change appropriately? 5. Visualizing electric field vectors Modify your previous work so that your script gives the field vector at your observation point. (You may set k = 1.) If you use the previously defined r and rhat it should take a very modest modification to go from an arrow representing ˆr to an arrow representing E. When you have this working, check the effect of varying: the source charge (magnitude and sign), the position of the source charge, and the observation point. Does the the displayed field vector change appropriately? 6. Python functions A Python function is a lot like a math function it takes one or more inputs called arguments, does something with them, and typically gives an output. In your IDLE editor define a function as follows: def myfunction(r): # lots of calculations could go here return 6*r print( myfunction(2) ) 4
Look in the Python shell window (where print results always go). Now try x=7 r=4 print( myfunction(x) ) Is the result what you expected? Explain in your own words what the role of the variable r is in the function. There s more than meets the eye with this function. We didn t tell the function what type of variable r was. Let s make a vector: v = vector(1,2,0) print( myfunction(v) ) Now look at the output. The function myfunction will return a number when the argument r is a number, but it returns a vector when the argument is a vector. This is allowed as long as the operations make sense (and is called dynamic typing). It is one of many ways that Python makes programming easier. Just for fun, see what Python does when you use the character "a" as the input to your function: print( myfunction("a") ) Delete these examples from your IDLE window and then define a function that returns the electric field at an observation point due to a given source charge. This shouldn t involve much more than cutting and pasting existing code. You function should look something like this: def e(obs,source): #your instructions here return... Check to make sure that your function gives the right vectors. 7. The electric field from multiple sources Now consider two charges, illustrated in Fig. 3, like those we studied on the second day of class. Use your electric field function to display the total field vector at the point (2,0,0). Check the field vector at some other points, like (4,0,0), (0,2,0), (0,-2,0),.... +1 q 1 = +10-1 q 2 = 10 Figure 3: Two charges 5
8. Submit your work Make sure to submit your work to my dropbox in facultystaff/m/mligare with the filename vp02_lastname.py when you re done. 9. Extras Reconsider the two charge example, and bump up the charges to ±20. Now create field vectors at 16 equally spaced points around a circle of radius 4. Then add field vectors at equally spaced points around a circle of radius 6. Think about the field lines that would represent the same vector field. Generalize the two charge example to situations in which there are many more sources. What features of Python will make this an easy task. Think about ways in which you might numerically approximate an integral for an line of charge.... 6